<?php

namespace Association\Controller;

use Think\Controller;

class ShareController extends CommonController {

    public function master() {

        $sql = M('Master');

        $count = $sql->count(); // 查询满足要求的总记录数
        $P = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $tpl = '<ul data-am-widget="pagination" class="am-pagination am-pagination-default am-fr">';
        $tpl .= '<li class="am-pagination-first ">%FIRST%</li>';
        $tpl .= '<li class="am-pagination-prev ">%UP_PAGE%</li>';
        $tpl .= '<li class="">%LINK_PAGE%</li>';
        $tpl .= '<li class="am-pagination-next ">%DOWN_PAGE%</li>';
        $tpl .= '<li class="am-pagination-last ">%END%</li>';
        $tpl .= '</ul>';

        $P->setConfig('header', '条记录');
        $P->setConfig('first', '首页');
        $P->setConfig('prev', '上一页');
        $P->setConfig('next', '下一页');
        $P->setConfig('last', '末页');
        $P->setConfig('theme', $tpl);

        $show = $P->show(); // 分页显示输出
        $list = $sql->limit($P->firstRow . ',' . $P->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);

        $this->display('./master-list');
    }

    public function product() {

        $sql = M('shareProduct');

        $count = $sql->count(); // 查询满足要求的总记录数
        $P = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $tpl = '<ul data-am-widget="pagination" class="am-pagination am-pagination-default am-fr">';
        $tpl .= '<li class="am-pagination-first ">%FIRST%</li>';
        $tpl .= '<li class="am-pagination-prev ">%UP_PAGE%</li>';
        $tpl .= '<li class="">%LINK_PAGE%</li>';
        $tpl .= '<li class="am-pagination-next ">%DOWN_PAGE%</li>';
        $tpl .= '<li class="am-pagination-last ">%END%</li>';
        $tpl .= '</ul>';

        $P->setConfig('header', '条记录');
        $P->setConfig('first', '首页');
        $P->setConfig('prev', '上一页');
        $P->setConfig('next', '下一页');
        $P->setConfig('last', '末页');
        $P->setConfig('theme', $tpl);

        $show = $P->show(); // 分页显示输出
        $list = $sql->alias('p')->join('left join l_share_seller s on s.id = p.sellerid')
                    ->join('left join l_master m on m.id = s.masterid')->field('p.*,s.truename,m.sitename')->limit($P->firstRow . ',' . $P->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);

        $this->display('./product-list');
    }

    public function seller() {

        $sql = M('shareSeller');

        $count = $sql->count(); // 查询满足要求的总记录数
        $P = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $tpl = '<ul data-am-widget="pagination" class="am-pagination am-pagination-default am-fr">';
        $tpl .= '<li class="am-pagination-first ">%FIRST%</li>';
        $tpl .= '<li class="am-pagination-prev ">%UP_PAGE%</li>';
        $tpl .= '<li class="">%LINK_PAGE%</li>';
        $tpl .= '<li class="am-pagination-next ">%DOWN_PAGE%</li>';
        $tpl .= '<li class="am-pagination-last ">%END%</li>';
        $tpl .= '</ul>';

        $P->setConfig('header', '条记录');
        $P->setConfig('first', '首页');
        $P->setConfig('prev', '上一页');
        $P->setConfig('next', '下一页');
        $P->setConfig('last', '末页');
        $P->setConfig('theme', $tpl);

        $show = $P->show(); // 分页显示输出
        $list = $sql->alias('s')->join('left join l_user u on u.uid = s.uid')
                    ->field('s.*,u.nickname')->limit($P->firstRow . ',' . $P->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);

        $this->display('./seller-list');
    }

    public function item() {

        $sql = M('shareItem');

        $count = $sql->count(); // 查询满足要求的总记录数
        $P = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)

        $tpl = '<ul data-am-widget="pagination" class="am-pagination am-pagination-default am-fr">';
        $tpl .= '<li class="am-pagination-first ">%FIRST%</li>';
        $tpl .= '<li class="am-pagination-prev ">%UP_PAGE%</li>';
        $tpl .= '<li class="">%LINK_PAGE%</li>';
        $tpl .= '<li class="am-pagination-next ">%DOWN_PAGE%</li>';
        $tpl .= '<li class="am-pagination-last ">%END%</li>';
        $tpl .= '</ul>';

        $P->setConfig('header', '条记录');
        $P->setConfig('first', '首页');
        $P->setConfig('prev', '上一页');
        $P->setConfig('next', '下一页');
        $P->setConfig('last', '末页');
        $P->setConfig('theme', $tpl);

        $show = $P->show(); // 分页显示输出
        $list = $sql->alias('i')->join('left join l_user u on u.uid = i.uid')
                    ->join('left join l_share_product p on p.id = i.productid')
                    ->join('left join l_share_seller s on s.id = p.sellerid')
                    ->join('left join l_master m on m.id = s.masterid')
                    ->field('i.*,p.productID,s.truename as sellername,m.sitename')->limit($P->firstRow . ',' . $P->listRows)->limit($P->firstRow . ',' . $P->listRows)->select();

        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);

        $this->display('./share-item-list');
    }

    function add() {

        $master = M('master');

        if (!IS_POST) {
            $this->display('./master-add');
        } else {

            if (!I('post.sitename')) {
                $this->error('对不起，请填写站点名称');
                exit();
            }

            if (!I('post.identifier')) {
                $this->error('对不起，请填写站点编号');
                exit();
            }

            if (!I('post.username')) {
                $this->error('对不起，请填写用户名');
                exit();
            }

            if (M('Master')->where("username = '" . I('post.username') . "'")->find()) {
                $this->error('对不起，已经存在该用户名');
                exit();
            }

            $data['sitename'] = I('post.sitename');
            $data['username'] = I('post.username');
            $data['password'] = md5(I('post.username'));
            $data['identifier'] = I('post.identifier');

            $master->add($data);

            $this->success("添加成功");
        }
    }

}
